package com.msoft.common.util;

public class ExceptionUtil {

	public static RuntimeException convertToRuntimeException(Throwable e) {
		if (e instanceof RuntimeException) {
			return (RuntimeException) e;
		}
		retainInterruptFlag(e);
		return new RuntimeException(e);
	}

	/**
	 * This sets the interrupt flag if the catched exception was an
	 * {@link InterruptedException}. Catching such an exception always clears
	 * the interrupt flag.
	 * 
	 * @param catchedException
	 *            The catched exception.
	 */
	public static void retainInterruptFlag(Throwable catchedException) {
		if (catchedException instanceof InterruptedException) {
			Thread.currentThread().interrupt();
		}
	}

	public static void rethrowInterruptedException(Throwable e)
			throws InterruptedException {
		if (e instanceof InterruptedException) {
			throw (InterruptedException) e;
		}
	}
}
